package com.znsd.zl.house.controller;

import com.znsd.zl.house.entity.HouseEntity;
import com.znsd.zl.house.entity.vo.HouseRespVO;
import com.znsd.zl.house.service.IHouseService;
import com.znsd.zl.utils.BaseController;
import com.znsd.zl.utils.PageUtils;
import com.znsd.zl.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @ClassName HouseController
 * @Description 描述
 * @author zhubin
 * @date 2020-08-19 22:54:37
 * @Version v1.0
 */
@Slf4j
@RestController
@RequestMapping("/zl/house")
public class HouseController extends BaseController<IHouseService, HouseEntity,Integer> {


	@GetMapping("/list")
	public R conditionQueryPage(HouseRespVO house, Integer page, Integer limit) {
		PageUtils pageUtils = service.conditionQueryPage(house, page, limit);
		return R.ok().put("page",pageUtils);
	}

	@GetMapping("/findByUserId")
	public R findByUserId(Integer userId, Integer page, Integer limit) {
		PageUtils pageUtils = service.findByUserId(userId, page, limit);
		return R.ok().put("page",pageUtils);
	}

	/**
	 * 添加信息
	 * @param t 数据库表对应实体类对象
	 * @return 失败返回500 ，成功0
	 */
	@PostMapping
	@Override
	public R save(@RequestBody HouseEntity t) {
		try {
			log.info("房屋录入的数据对象 =====" + t);
			boolean b = service.save(t);
			System.out.println(t);
			return b ? R.ok() : R.error("添加失败");
		}catch (Exception e){
			e.printStackTrace();
			return R.error();
		}
	}

	/**
	 * 查询一个房源的状态
	 * @param houseId：房源ID
	 * @return R ：返回的结果集
	 */
	@GetMapping("findHouseStatus")
	public R findHouseStatus(@RequestParam Integer houseId){
		Map<String, Object> map = service.findHouseStatus(houseId);
		return R.ok().put("map",map);
	}

	@DeleteMapping
	@Override
	public R deleteBatch(@RequestBody List<Integer> ids) {
		try {
			log.info("【通用Controller】批量删除成功，删除id：{}", ids);
			this.service.deleteBatch(ids);
		}catch (Exception e){
			e.printStackTrace();
			return R.error("服务器异常，请稍后重试");
		}

		return R.ok();
	}


}
